/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2015年1月7日         cassie
 */

package com.coocaa.launcher.pattern.normal.allapp;

import android.content.Context;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.coocaa.x.uipackage.SkinManager;
import com.coocaa.x.uipackage.SkinManager.OnThemeChangeListener;
import com.tianci.appstore.R;
import com.tianci.appstore.ui.widget.CatchImageView;
import com.tianci.appstore.utils.SkyConstants;

public class IconButton extends LinearLayout implements OnThemeChangeListener {
    private Context mContext;
    private ImageView icon;
    private TextView text;
    private int iconId = 0;

    public IconButton(Context context) {
        super(context);
        this.mContext = context;
        // TODO Auto-generated constructor stub
        createView();
        setEnabled(true);
    }

    private void createView() {
        this.setOrientation(VERTICAL);
        this.setGravity(Gravity.CENTER);
        icon = new CatchImageView(mContext);
        int iconSize = SkyConstants.Div(138);
        this.addView(icon, new LayoutParams(iconSize, iconSize));

        text = new TextView(mContext);

        text.setTextSize(SkyConstants.Dpi(29));
        text.setTextColor(SkinManager.getInstance().getColor(R.color.appitemview_title_textcolor));
        text.setGravity(Gravity.CENTER);
        text.setSingleLine();
        LayoutParams textParams = new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT);
        textParams.topMargin = SkyConstants.Div(9);
        this.addView(text);
    }

    public void setIcon(int resId) {
        this.iconId = resId;
        SkinManager.getInstance().setBackgroundResource(icon, resId);
    }

    public void setText(int strId) {
        text.setText(strId);
    }

    @Override
    public void onThemeChange() {
        // TODO Auto-generated method stub
        if (iconId != 0)
            SkinManager.getInstance().setBackgroundResource(icon, iconId);
    }

    @Override
    public void setEnabled(boolean enabled) {
        // TODO Auto-generated method stub
        setFocusable(enabled);
        setFocusableInTouchMode(enabled);
        if (enabled)
            this.setAlpha(1.0f);
        else
            this.setAlpha(0.5f);
        super.setEnabled(enabled);
    }

}
